package com.gnerv.rabbitmq.agent.command;

import com.gnerv.rabbitmq.agent.model.entity.Node;
import com.gnerv.rabbitmq.agent.tools.ExecTools;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
public class PolicyCommand extends AbstractRabbitmqctlCommand {

    public PolicyCommand(Node node) {
        super();
        setNode(node);
    }

    public static PolicyCommand build(Node node) {
        return new PolicyCommand(node);
    }

    public ExecTools.ExecResult listPolicies() {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("list_policies");
        return run(listExchanges);
    }


    public ExecTools.ExecResult listOperatorPolicies() {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("list_operator_policies");
        return run(listExchanges);
    }

    public ExecTools.ExecResult setOperatorPolicy(String vhost,
                                                  String priority,
                                                  String applyTo,
                                                  String name,
                                                  String pattern,
                                                  String definition) {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("set_operator_policy");
        listExchanges.add("-p");
        listExchanges.add(vhost);
        listExchanges.add("--priority");
        listExchanges.add(priority);
        listExchanges.add("--apply-to");
        listExchanges.add(applyTo);
        listExchanges.add(name);
        listExchanges.add(pattern);
        listExchanges.add(definition);
        return run(listExchanges);
    }

    public ExecTools.ExecResult setPolicy(String vhost,
                                          String priority,
                                          String applyTo,
                                          String name,
                                          String pattern,
                                          String definition) {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("set_policy");
        listExchanges.add("-p");
        listExchanges.add(vhost);
        listExchanges.add("--priority");
        listExchanges.add(priority);
        listExchanges.add("--apply-to");
        listExchanges.add(applyTo);
        listExchanges.add(name);
        listExchanges.add(pattern);
        listExchanges.add(definition);
        return run(listExchanges);
    }

    public ExecTools.ExecResult clearPolicy(String vhost, String name) {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("clear_policy");
        listExchanges.add("-p");
        listExchanges.add(vhost);
        listExchanges.add(name);
        return run(listExchanges);
    }

    public ExecTools.ExecResult clearOperatorPolicy(String vhost, String name) {
        List<String> listExchanges = Lists.newArrayList();
        listExchanges.add("clear_operator_policy");
        listExchanges.add("-p");
        listExchanges.add(vhost);
        listExchanges.add(name);
        return run(listExchanges);
    }

}
